#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QUndoStack>
#include <QFileDialog>
#include <QLineEdit>
#include <QComboBox>
#include <QRegExpValidator>

#include "ImportModelDlg.h"
#include "Registry.h"
#include "mainwindow.h"
#include "ActorTable.h"
#include "CmdManager.h"
#include "InfoWindow.h"
#include "Scene.h"
#include "OsgUtil.h"
#include "QtUtility.h"
#include "Operation/OpAdd.h"
#include "WidgetUtil.h"
#include "QtOSGWidget.h"
#include "SendPointHandler3D.h"

ImportModelDlg::ImportModelDlg(QWidget * parent, Qt::WindowFlags f)
	: DlgBase(parent, f)
{
	setWindowTitle(tr("导入模型"));
	auto mainLayout = new QVBoxLayout;

	{
		auto hbox = new QHBoxLayout;

		auto depthLabel = new QLabel(tr("导入模型长度单位: "));
		hbox->addWidget(depthLabel);
		m_unixCB = new QComboBox;
		m_unixCB->addItem(QObject::tr("米"));
		m_unixCB->addItem(QObject::tr("厘米"));
		m_unixCB->addItem(QObject::tr("毫米"));
		hbox->addWidget(m_unixCB);

		auto merge = createCheckBox("合并同平面相邻三角形", "merge");
		merge->setChecked(true);
		hbox->addWidget(merge);

		hbox->addStretch(1);
		mainLayout->addLayout(hbox);
	}

	auto noteLabel = new QLabel(tr("注意：尽量使用stp/igs/brp/brepjson文件。<br>osgb,osgt,ply,fbx文件只适用于构件表面全部是<b>无孔洞无曲线边</b>的<b>平面</b>。"));
	mainLayout->addWidget(noteLabel);
	{
		auto hbox = new QHBoxLayout;
		QRegExp regExp("[^\\\\/:*?\"<>|]+");
		QRegExpValidator *validator = new QRegExpValidator(regExp, this);
		m_filePathLE = createLineEdit(hbox, "fileName", tr("文件名"), "", validator);
		m_filePathLE->setMinimumWidth(312);
		hbox->setStretchFactor(m_filePathLE, 1);
		auto btn = new QPushButton;
		btn->setText("...");
		btn->setStyleSheet(R"(QPushButton {
	padding: 4px 0px;
	margin: 0px 0px;
})");
		hbox->addWidget(btn);
		connect(btn, &QPushButton::clicked, this, &ImportModelDlg::fillFileName);

		hbox->addStretch(1);
		mainLayout->addLayout(hbox);
	}

	//确认、取消按钮
	auto buttonLayout = new QHBoxLayout;
	buttonLayout->addStretch(1);
	auto confirmButton = new QPushButton(tr("导入"));
	confirmButton->setDefault(true);
	confirmButton->setFixedSize(60, 30);
	buttonLayout->addWidget(confirmButton);
	auto cancelButton = new QPushButton(tr("取消"));
	cancelButton->setFixedSize(60, 30);
	buttonLayout->addWidget(cancelButton);
	mainLayout->addLayout(buttonLayout);
	connect(confirmButton, &QPushButton::clicked, this, &ImportModelDlg::confirm);
	connect(cancelButton, &QPushButton::clicked, this, &ImportModelDlg::cancel);
	setLayout(mainLayout);
}

ImportModelDlg::~ImportModelDlg()
{
	
}

void ImportModelDlg::fillFileName()
{
	static QString lastDir(".");
	auto fileName = QFileDialog::getOpenFileName(nullptr, tr("导入模型"), lastDir, tr("三维构件模型(*.step *.stp *.iges *.igs *.brep *.brp *.brep.json *.brepjson *.osgb *.osgt *.ply *.fbx)"));
	if (fileName.isEmpty()) // user canceled
		return;
	QFileInfo fileInfo(fileName);
	lastDir = fileInfo.absolutePath();

	setLineEditText(this, "fileName", fileName);
}

int getLeafCount(IRoot* root);
std::set<std::shared_ptr<INode>> importModel(Scene* scene, const QString& filePath, double scale, bool merge);
void ImportModelDlg::confirm()
{
	double scale = 1.0;
	int index = m_unixCB->currentIndex();
	switch (index)
	{
	case 1:
		scale = 0.01;
		break;
	case 2:
		scale = 0.001;
		break;
	}
	auto fileName = m_filePathLE->text();
	if (fileName.isEmpty())
		return;

	bool merge = isCheckBoxChecked(this, "merge");

	Registry &reg = Registry::instance();
	auto scene = reg.get<Scene>(constructSceneID);
	auto result = ::importModel(scene, fileName, scale, merge);
	if (!result.empty())
	{
		OpAdd* opAdd = new OpAdd(result);
		auto undoStack = scene->undoStack();
		undoStack->push(opAdd);
		auto widget3D = reg.get<QtOSGWidget>(widget3DID);
		auto root = widget3D->getIRoot();
		int leafCount = getLeafCount(root);
		widget3D->getPointSender()->setSnapPointFlag(leafCount < 10000 ? SnapPointObject  : 0);
		close();
	}
}

void ImportModelDlg::cancel()
{
	close();
}
